import qs from "qs"


global.BASEURI = undefined
global.NETMETHODGET = 'GET'
global.NETMETHODPOST = 'POST'
global.NETHEADERS = {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    "szw": "00000000000000000000"
}

const option = {
    headers:NETHEADERS
}

export default class Net {

    static async get(path, params) {
        if (BASEURI == undefined) {
            console.warn("未设置BASEURI")
            return undefined
        }
        url = BASEURI + path + "?" + qs.stringify(params)
        let res = await fetch(url, { ...option, method: NETMETHODGET })
        let json = await res.json().catch(e=>{
            return {status:res.status}
        })
        return json
    }

    static async post(path, body) {
        if (BASEURI == undefined) {
            console.warn("未设置BASEURI")
            return undefined
        }
        url = BASEURI + path
        let res = await fetch(url, { ...option, method: NETMETHODPOST, body: JSON.stringify(body) })
        let json = await res.json().catch(e=>{
            return {status:res.status}
        })
        return json
    }

    static async third_fetch(url, method, params, body) {
        let op = { ...option }
        if (method == NETMETHODGET) {
            op = { ...op, method }
            url += qs.stringify(params)
        }
        if (method == NETMETHODPOST) {
            op = { ...op, method, body: JSON.stringify(body) }
        }
        let res = await fetch(url, op)
        let json = await res.json().catch(e=>{
            return {status:res.status}
        })
        return json
    }
}
